package com.wenx.concurrent.pk;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolPkTest {


    public static void main(String[] args) throws InterruptedException {

        ExecutorService executorService = Executors.newSingleThreadExecutor();

        final List<Integer> list = new ArrayList<>();

        Instant start = Instant.now();

        Random random = new Random();
        for (int i = 0; i < 10000; i++) {
            executorService.execute(() -> {
                list.add(random.nextInt());
            });
        }
        //关闭线程池
        executorService.shutdown();
        //等待线程执行完成
        executorService.awaitTermination(1, TimeUnit.MINUTES);

        Instant end = Instant.now();
        System.out.println("times = " + Duration.between(start,end).toMillis());
        System.out.println(list.size());
    }

}
